<?php
namespace Yzadmin\Controller;
use Common\Controller\AdminBaseController;
/**
 * 后台管理员组控制器
 */
class AuthgroupController extends AdminBaseController {

  public function index(){
    $list = D('AuthGroup')->where('status=1')->select();
    foreach ($list as $key=>$val){
      $root = $val['rules'];$rules_char = '';
      if($root){
        $days_array=explode(',',$root);
        foreach($days_array as $val){
          $rules_char .= get_row('权限',$val,'title').'，';
        }
      }
      $list[$key]['rules'] = $rules_char;
    }
    $assign=array(
      'lists'=>$list,
    );
    $this->assign($assign);
    $this->display();
  }

    public function add()
    {
        if (IS_POST){
            $data = I('post.data');
            $rule_ids = I('post.rule_ids');$rule_ids = implode(',',$rule_ids);
            $data['rules'] = $rule_ids;
            $result = D('AuthGroup')->add($data);
            if(false !== $result){
              $this->success('数据操作成功！',U('index'));exit;
            }else{
              $this->error('服务器正忙，请稍后再试！');
            }
        }

        $this->assign('cateType',D('AuthGroup')->select());
        // 获取用户组数据
        $group_data=M('Auth_group')->where(array('id'=>$id))->find();
        $group_data['rules']=explode(',', $group_data['rules']);
        // 获取规则数据
        $rule_data=D('AuthRule')->getTreeData('level','id','title');
        $assign=array(
            'group_data'=>$group_data,
            'rule_data'=>$rule_data
        );
        $this->assign($assign);
        $this->display('edit');
    }



    public function edit()
    {
         if (IS_POST){
            $data = I('post.data');$id = intval(I('post.id'));
            if(empty($id)){$this->error('服务器正忙，请稍后再试！');}
            $rule_ids = I('post.rule_ids');$rule_ids = implode(',',$rule_ids);
            $data['rules'] = $rule_ids;
            $result = D('AuthGroup')->where(array('id'=>$id))->save($data);
            if(false !== $result){
              $this->success('数据操作成功！',U('index'));exit;
            }else{
              $this->error('服务器正忙，请稍后再试！');
            }
        }
        $id=I('get.id');
        if (empty($id)){$this->error('非法操作');}
        $info=D('AuthGroup')->find($id);
        // 获取用户组数据
        $group_data=M('Auth_group')->where(array('id'=>$id))->find();
        $group_data['rules']=explode(',', $group_data['rules']);
        // 获取规则数据
        $rule_data=D('AuthRule')->getTreeData('level','id','title');
        $assign=array(
          'id'=>$id,
          'info'=>$info,
          'group_data'=>$group_data,
          'rule_data'=>$rule_data
        );
        $this->assign($assign);
        $this->display();
    }



    public function del($id = 0){
      if(IS_POST){
        if(empty($id)){echo 0;exit;}
        $result2 = D('Auth_group_access')->where('group_id='.$id)->delete();
        $result = D('AuthGroup')->where('id='.$id)->delete();
        if($result){
          echo 1;exit;
        }else{
          echo 0;exit;
        }
      }
    }
}
